﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
   public class Sudoku
    {
        int [,] mas  = new int [9, 9];
        bool[,] masb = new bool[9, 9];


        bool[,] duplicates = new bool[9, 9];
        
        public int[,] ReadInformation(string filename)
        {
            string info;
            
            StreamReader sr = new StreamReader(filename);
            
            int i = 0;
            while (!sr.EndOfStream)
            {
                info = sr.ReadLine();
                string[] s = info.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                for (int j = 0; j < 9; j++)
                {
                    mas[i, j] = int.Parse(s[j]);
                }
                i++;
            }
            sr.Close();
            masb = BoolCreate(mas);
            return mas;
       }

        public bool[,] BoolCreate(int[,] m)
        {
            bool[,] Mbool = new bool[9, 9];
            for (int i = 0; i < 9; i++)

                for (int j = 0; j < 9; j++)
                {
                    if (m[i, j] != 0)
                        Mbool[i, j] = false;
                    else
                        Mbool[i, j] = true;
                }
            return Mbool;

        }
        public bool SetValue(int x, int y, int value)
        {
            duplicates = new bool[9, 9];
            bool flag = true;
            if (value == 0)
            {
                flag = false;
            }
            for (int i = 0; i < 9; i++)
                if (Mas[y, i] == value)
                {
                    duplicates[y, i] = true;
                    flag = false;
                }

            for (int i = 0; i < 9; i++)
                if (Mas[i, x] == value)
                {
                    duplicates[i, x] = true;
                    flag = false;
                }                

            int xstart = (x / 3) * 3;
            int ystart = (y / 3) * 3;

            for (int i = ystart; i < ystart + 3; i++)
                for (int j = xstart; j < xstart + 3; j++)
                    if (Mas[i, j] == value)
                    {
                        duplicates[i, j] = true;
                        flag = false;
                    }

            return flag;
        }

        public bool[,] Duplicates
        {
            get
            {
                return duplicates;
            }
        }

        public int[,] Mas
        {
            set
            {
                mas = value;
            }
            get
            {
                return mas;
            }
        }

        public bool[,] Masb
        {
            get
            {
                return masb;
            }
        }

    }
}
